package com.swlz.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.swlz.commons.DBUtil;
import com.swlz.entity.Recharge;

import java.util.Date;
/**
 * <pre>
 * 数据访问对象
 * 充值记录
 * TableName : SYS_RECHARGE
 * ClassName : com.swlz.dao.RechargeDAO
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-06-25 07:25:16
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class RechargeDAO {

	/**
	 * 添加一个充值记录
	 * 包含所有字段
	 * @param recharge - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insert(Recharge recharge) throws SQLException {
		if(recharge.getRechargeId()==null) {
			recharge.setRechargeId(DBUtil.nextId());
		}
		return DBUtil.insert(recharge);
	}
 
	
	/**
	 * 添加一个充值记录
	 * 只包含非null的字段
	 * @param recharge - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insertSelective(Recharge recharge) throws SQLException {
		if(recharge.getRechargeId()==null) {
			recharge.setRechargeId(DBUtil.nextId());
		}
		return DBUtil.insertSelective(recharge);
	}
	

	/**
	 * 根据主键更新一个充值记录
	 * 包含所有字段
	 * @param recharge - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int update(Recharge recharge) throws SQLException {
		return DBUtil.update(recharge);
	}
	
	/**
	 * 根据主键更新一个充值记录
	 * 只包含非null的字段
	 * @param recharge - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int updateSelective(Recharge recharge) throws SQLException {
		return DBUtil.updateSelective(recharge);
	}
	
	/**
	 * 根据主键删除一个充值记录
	 * @param rechargeId - 主键
	 * @return
	 * @throws SQLException
	 */
	public int deleteById(Long rechargeId) throws SQLException  {
		return DBUtil.execute("DELETE FROM SYS_RECHARGE WHERE RECHARGE_ID=?", rechargeId);
	}
	
	/**
	 * 根据主键获取一个充值记录
	 * @param rechargeId - 主键
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public Recharge selectById(Long rechargeId) throws SQLException  {
		return DBUtil.get("SELECT * FROM SYS_RECHARGE WHERE RECHARGE_ID=?", Recharge.class ,rechargeId);
	}

		
	/**
	 * 根据模型中的非空字段查询
	 * @param rechargeId - 实体对象
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public Recharge selectByModel(Recharge recharge)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM SYS_RECHARGE WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(recharge,sql,params);
		return DBUtil.get(sql.toString(), Recharge.class, params.toArray());
	}
	
	/**
	 * 根据模型中的非空字段查询
	 * @param rechargeId - 实体对象
	 * @param index - 开始索引
	 * @param len - 返回长度
	 * @return
	 * @throws SQLException
	 */
	public List<Recharge> selectByModel(Recharge recharge,int index,int len)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM SYS_RECHARGE WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(recharge,sql,params);
		sql.append(" ORDER BY RECHARGE_ID DESC LIMIT ?,?");
		params.add(index);
		params.add(len);
		return DBUtil.list(sql.toString(), Recharge.class, params.toArray());
	}

	/**
	 * 根据模型中的非空字段统计
	 * @param rechargeId - 实体对象
	 * @return
	 * @throws SQLException
	 */
	public long countByModel(Recharge recharge)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT count(*) as c FROM SYS_RECHARGE WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(recharge,sql,params);
		return DBUtil.getLong(sql.toString(), params.toArray());
	}
	
	/**
	 * 格式化where条件和参数
	 * @param rechargeId - 实体对象
	 * @param sql - SQL语句
	 * @param params - SQL语句中的参数
	 */
	private void where(Recharge recharge,StringBuffer sql,List<Object> params) {
		if(recharge==null){
			return;
		}

		if(recharge.getRechargeId()!=null) {
			sql.append(" AND RECHARGE_ID=?");
			params.add(recharge.getRechargeId());
		}

		if(recharge.getUserId()!=null) {
			sql.append(" AND USER_ID=?");
			params.add(recharge.getUserId());
		}

		if(recharge.getRechargeMoney()!=null) {
			sql.append(" AND RECHARGE_MONEY=?");
			params.add(recharge.getRechargeMoney());
		}

		if(recharge.getRechargeScore()!=null) {
			sql.append(" AND RECHARGE_SCORE=?");
			params.add(recharge.getRechargeScore());
		}

		if(recharge.getRechargePayTypeId()!=null) {
			sql.append(" AND RECHARGE_PAY_TYPE_ID=?");
			params.add(recharge.getRechargePayTypeId());
		}

		if(recharge.getRechargePayId()!=null) {
			sql.append(" AND RECHARGE_PAY_ID=?");
			params.add(recharge.getRechargePayId());
		}

		if(recharge.getRechargeTime()!=null) {
			sql.append(" AND RECHARGE_TIME=?");
			params.add(recharge.getRechargeTime());
		}

		if(recharge.getRechargeStatus()!=null) {
			sql.append(" AND RECHARGE_STATUS=?");
			params.add(recharge.getRechargeStatus());
		}

	}
}